<?php

include_once(dirname(__FILE__) . "../../TMS_TEDxCairo_AOP/AOP.php");
include_once(dirname(__FILE__) . "../../TMS_TEDxCairo_DATABASE_DAO/CMSDAO.class.php");

$cmsDAO = new CMSDAO();

/**
 * Section part
 */
if ($_REQUEST["action"] == "section") {
    $sections = $cmsDAO->getSections();
    include ('views/CMS/sectionIndex.php');
}

if ($_REQUEST["action"] == "createSection") {
    if (isset($_REQUEST["id"])) {
        $section = $cmsDAO->getSection($_REQUEST["id"]);
    }
    $sectionTypes = $cmsDAO->getSectionTypes();
    include ('views/CMS/createSection.php');
    exit;
}
if ($_REQUEST["action"] == "saveSection") {
    if (isset($_REQUEST['id'])) {
        $cmsDAO->editSection($_REQUEST['name'], $_REQUEST['type'], $_REQUEST['status'], $_REQUEST['order'], $_REQUEST['mainSubsection'], $_REQUEST['id']);
    }
    else
        $cmsDAO->saveSection($_REQUEST['name'], $_REQUEST['type'], $_REQUEST['status'], $_REQUEST['order']);
    echo "<div style='font-size:16px;'>Section " . $_REQUEST['name'] . " Saved successfully</div>";
}

/**
 * Subsection part
 */
if ($_REQUEST["action"] == "subsection") {
    $subsections = $cmsDAO->getSubsections();
    include ('views/CMS/subsectionIndex.php');
}

if ($_REQUEST["action"] == "createSubsection") {
    if (isset($_REQUEST["id"])) {
        $subsection = $cmsDAO->getSubsection($_REQUEST["id"]);
    }
    $subsectionTypes = $cmsDAO->getSubsectionTypes();
    $sections = $cmsDAO->getSections();
    include ('views/CMS/createSubsection.php');
    exit;
}
if ($_REQUEST["action"] == "saveSubsection") {
    if (isset($_REQUEST['id'])) {
        $cmsDAO->editSubsection($_REQUEST['name'], $_REQUEST['type'], $_REQUEST['status'], $_REQUEST['order'], $_REQUEST['mainContent'], $_REQUEST['section'],$_REQUEST['id']);
    }
    else
        $cmsDAO->saveSubsection($_REQUEST['name'], $_REQUEST['type'],$_REQUEST['section'], $_REQUEST['status'], $_REQUEST['order']);
    echo "<div style='font-size:16px;'>Subsection " . $_REQUEST['name'] . " Saved successfully</div>";
}

/**
 * Content Part
 */

if ($_REQUEST["action"] == "content") {
    $contents = $cmsDAO->getContents();
    include ('views/CMS/contentIndex.php');
}

if ($_REQUEST["action"] == "createContent") {
    if (isset($_REQUEST["id"])) {
        $content = $cmsDAO->getContent($_REQUEST["id"]);
    }
    $sections = $cmsDAO->getSections();
    $subsections = $cmsDAO->getSectionsSubsections($content->subsection->section->id);
    include ('views/CMS/createContent.php');
    exit;
}

if ($_REQUEST["action"] == "saveContent") {
    $desc = htmlspecialchars($_REQUEST['content']);
    if (isset($_REQUEST['id'])) {
        $cmsDAO->editContent($_REQUEST['name'],$_REQUEST['subsection'], $_REQUEST['status'], $_REQUEST['order'], $desc, $_REQUEST['id']);
    }
    else
        $cmsDAO->saveContnet($_REQUEST['name'], $_REQUEST['subsection'],$desc,$_REQUEST['status'], $_REQUEST['order']);
    
    echo "<div style='font-size:16px;'>Content " . $_REQUEST['name'] . " Saved successfully</div>";
}

if ($_REQUEST["action"] == "getSubsections") {
    $subsections = $cmsDAO->getSectionsSubsections($_REQUEST['id']);
    echo "<select name='subsection' validate[required]>";
    foreach($subsections as $subsection){
        echo "<option value='{$subsection->id}'>{$subsection->name}</option>";
    }
    echo "</select>";
}


?>
